package cn.wen.yaling.shiro.token;

import cn.wen.yaling.shiro.utils.JwtUtils;
import lombok.Data;
import org.apache.shiro.authc.AuthenticationToken;
import org.springframework.stereotype.Component;


@Data
@Component
public class UserToken implements AuthenticationToken {
    private String token;
    private String phone;
    private char[] code;

    public UserToken() {
    }

    public UserToken(String token) {
        this.token = token;
    }

    public UserToken(String token, char[] code) {
        this(token, (char[]) code, null);
    }

    public UserToken(String token, char[] code, String phone) {
        this.token = token;
        this.phone = phone;
        this.code = code;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.getClass().getName());
        sb.append(" - ");
        sb.append(JwtUtils.parseJWT(this.token).getId());
        sb.append(", username=").append(this.phone);
        return sb.toString();
    }

    @Override
    public Object getPrincipal() {
        return phone;
    }

    @Override
    public Object getCredentials() {
        return phone;
    }
}
